package toy.keli.edic.ui.bookmark

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import toy.keli.edic.data.GlobalVal
import toy.keli.edic.data.model.Category
import toy.keli.edic.ui.bookmark.components.BookGrid
import toy.keli.edic.ui.bookmark.components.BookList
import toy.keli.edic.ui.bookmark.data.BookItem
import toy.keli.edic.ui.bookmark.data.BookMark
import toy.keli.edic.ui.bookmark.data.BookMark.Companion.clickModel
import toy.keli.edic.ui.bookmark.data.BookMark.clickModelItem
import toy.keli.edic.ui.main.components.AddButton
import toy.keli.edic.ui.main.components.Confirm
import toy.keli.edic.ui.main.components.Display
import toy.keli.edic.ui.main.components.Display.mt
import toy.keli.edic.ui.main.components.InputDialog
import toy.keli.edic.ui.main.components.Prompt
import toy.keli.edic.util.ActivityRun
import toy.keli.edic.util.ScreenUtil
import toy.keli.edic.view.MPopMenu
import toy.keli.edic.ui.main.extend.MText as Text

var title by mutableStateOf("Books")
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun BookmarkScreen() {
    val titleMenu by remember {
        mutableStateOf(MPopMenu(arrayListOf()))
    }
    val addMenu = remember {  MPopMenu.BookAddMenu().apply { offset = DpOffset(Display.px2dp(Display.getScreenSize().x)-150.dp ,0.dp)  } }
    remember {
        // 显示确认对话框
        BookMark.onChangePid { pid ->
            if (GlobalVal.pid != pid) {
                BookMarkEvent.Run.upBookItems()
                GlobalVal.pid = pid
            }
            if (pid == 0) {
                title = "Books"
                titleMenu.upItems(arrayListOf())
                return@onChangePid
            }
            val ps = Category.getParents(pid)
            if(ps.size==0) return@onChangePid
            val nowMenu = ps[0]
            val bmChild = arrayListOf<MPopMenu.dataClass>()
            title = nowMenu.name
            ps.subList(1, ps.size).forEach {
                bmChild.add(MPopMenu.dataClass(it.name, value = it.id))
            }
            bmChild.add(MPopMenu.dataClass("Books", value = 0))
            titleMenu.upItems(bmChild)

            ActivityRun.onBackPressed {
                if(clickModel!=clickModelItem.click){
                    clickModel=clickModelItem.click
                    false
                }else {
                    val pid = BookMark.pid
                    if (BookMark.pid != 0) {
                        BookMark.changePid(nowMenu.pid)
                    }
                    pid == 0
                }
            }
        }
        BookMark.onChangePid()
        BookMark.pid

    }
    Confirm.add()
    Prompt.add()
    Surface(color = MaterialTheme.colorScheme.background) {
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
            ) {
                Row (
                    Modifier
                        .fillMaxWidth()
                        .padding(start = 16.dp, end = 16.dp, bottom = 16.dp, top = 16.dp),
                    verticalAlignment = Alignment.CenterVertically
                ){
                    Text(
                        text = mt(title),
                        style = MaterialTheme.typography.headlineMedium,
                        color = MaterialTheme.colorScheme.onBackground,
                        modifier = Modifier
                            .combinedClickable(
                                onClick={
                                    titleMenu.show{ _, v->
                                        BookMark.changePid(v.value as Int)
                                    }
                                },
                                onLongClick = {
                                    GlobalVal.isSearchVisible.value = true
                                }
                            )
                    )
                    titleMenu.add()
                    Spacer(
                        modifier = Modifier
                            .weight(1f)
                            .fillMaxWidth()
                    )
                    when(BookMark.clickModel){
                        BookMark.clickModelItem.isSelect -> {
                            Button(onClick = { BookMarkEvent.onCheckAll(BookMark.bookItems) }) {
                                Text("全选")
                            }
                            Spacer(Modifier.width(5.dp))
                            Button(onClick = {
                                BookMark.clickModel = BookMark.clickModelItem.isCanCopy
                                BookMarkEvent.cutData = BookMark.bookItems.filter { it.isSelect }

                                if(BookMark.pid==-1) BookMark.changePid(0)
                            }) {
                                Text("剪切")
                            }
                            Spacer(Modifier.width(5.dp))
                            Button(onClick = {
                                BookMarkEvent.onDeleteItems(BookMark.bookItems.filter { it.isSelect })
                            }) {
                                Text("删除")
                            }
                            Spacer(Modifier.width(5.dp))
                        }
                        BookMark.clickModelItem.isCanCopy -> {
                            Button(onClick = { BookMarkEvent.onPaste(BookMark.pid) }) {
                                Text("粘贴")
                            }
                            Spacer(Modifier.width(5.dp))
                            Button(onClick = { BookMark.clickModel = BookMark.clickModelItem.click }) {
                                Text("取消")
                            }
                            Spacer(Modifier.width(5.dp))
                        }
                        else ->{

                        }
                    }
                    addMenu.add()
                    AddButton(
                        menu =addMenu,
                        onLongClick ={
                            InputDialog.show()
                        },
                        onClick = {
                            BookMarkEvent.AddButtonOnLongClick(it)
                        }
                    )

                }
                Row {
                    if(BookMark.pid==-1) {
                        BookList()
                    }else {
                        BookGrid()
                    }
                }
            }
    }
    InputDialog.add("创建分类","输入你的分类名字或文章名字:",{
        if(InputDialog.eObj!=null) {
            val item = InputDialog.eObj as BookItem
            item.save(content=it)
            BookMarkEvent.Run.upBookItems()
        }else {
            BookMarkEvent.onAdd(it, BookMark.pid)
        }
    })

//    InfoDialog.add()
}
